var options = {
    qingfanqie_button: {
        type: 'bool',
        defval: true
    },
    nationallib_button: {
        type: 'bool',
        defval: true
    },
    imdb_rating: {
        type: 'bool',
        defval: true
    },
    douban_album_previews: {
        type: 'bool',
        defval: true
    },
    doulist_import: {
        type: 'bool',
        defval: true
    },
    douban_event_sync: {
        type: 'bool',
        defval: true
    },
    douban_event_calendar_name: {
        type: 'string',
        defval: '豆瓣活动'
    },
    douban_event_last_sync_time: {
        type: 'string',
        readonly: true
    },
    douban_event_map: {
        type: 'bool',
        defval: true
    },
    douban_auto_paginator: {
        type: 'bool',
        defval: true
    },
    douban_auto_scroll: {
        type: 'bool',
        defval: true
    },
    douban_api_key: {
        type: 'string',
        defval: '059fda1fed12c97d10df8e5fc7385c77',
        readonly: true
    },
    douban_api_key_secret: {
        type: 'string',
        defval: '291defb0d91838fc',
        readonly: true
    },
    douban_access_token: {
        type: 'string',
        readonly: true
    },
    douban_access_token_secret: {
        type: 'string',
        readonly: true
    },
    google_login_token: {
        type: 'string',
        readonly: true
    },
    google_calendar_button: {
        type: 'bool',
        defval: true
    },
    qrcode_chart: {
        'type': 'bool',
        defval: true
    }
};

function init_options() {
    for (var key in options) {
        var option = options[key];

        if (option.type == undefined) continue;

        option.key = key;
        option.value = window.localStorage.getItem(key);

        if ((option.value == undefined || option.value == null) && option.defval) {
            option.value = option.defval;

            window.localStorage.setItem(key, option.value);

            console.log("set option " + key + " to default value " + option.defval);
        } else if (option.type == 'bool') {
            option.value = option.value == 'true';
        }

        console.log("load option " + key + " = " + option.value + " default = " + option.defval);
    }
}

function read_option(key, callback) {
    if (callback) {
        chrome.extension.sendRequest({
            action: 'option.read',
            key: key
        }, function (response) {
            console.log("read option " + response.key + " = " + response.value);

            callback(response.key, response.value);
        });
    } else {
        if (options[key] == undefined) return null;
        
        var value = window.localStorage.getItem(key) || options[key].defval;

        if (options[key].type == 'bool') {
            value = value == 'true';
        }

        return value;
    }
}

function write_option(key, value, callback) {
    if (callback) {
        chrome.extension.sendRequest({
            action: 'option.write',
            key: key,
            value: value
        }, function (response) {
            console.log("write option " + response.key + " = " + response.value);

            callback(response.key, response.value);
        });
    } else {
        window.localStorage.setItem(key, value);
    }
}

function delete_option(key, callback) {
    if (callback) {
        chrome.extension.sendRequest({
            action: 'option.delete',
            key: key
        }, function (response) {
            console.log("delete option " + response.key);

            callback(response.key);
        });
    } else {
        window.localStorage.removeItem(key);
    }
}